package cn.wolfcode.trip.core.util;

import cn.wolfcode.trip.core.domain.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

//用户的上下文对象
public class UserContext {

    public static final String USER_IN_SESSION = "USER_IN_SESSION";


    //获取session
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    //共享当前用户
    public static void setCurrentUser(User currentUser){
        getSession().setAttribute(USER_IN_SESSION,currentUser);
    }

    //获取当前用户
    public static User getCurrentUser(){
        return (User) getSession().getAttribute(USER_IN_SESSION);
    }


}
